import React, { Component } from 'react';
import PropTypes from 'prop-types';
import MaterialTable from 'material-table';
import UnitsAPIModel from '../../../../../zoo_api/common/models/UNITS.json';
import TableColumnHelper from '../../../util/TableColumnHelper';
import { Units as UnitsAPI } from '../../../api';
import { Notifications } from '../../../components';
export default class units extends Component {
static async getInitialProps({ authToken }) {
const serverUnitsAPI = new UnitsAPI(authToken);
try {
const res = await serverUnitsAPI.getUnits().catch((err) => ({ data: [{ err: true, msg: err }] }));
return { allUnits: res.data };
} catch (err) {
return {
allUnits: [],
error: true,
errorMessage: 'Error loading units from DB',
};
}
}
static propTypes = {
token: PropTypes.string,
allUnits: PropTypes.arrayOf(PropTypes.object).isRequired,
};
static defaultProps = {
token: '',
}
constructor(props) {
super(props);
const keys = Object.keys(UnitsAPIModel.properties);
const UnitsColumns = {};
keys.forEach((key) => { UnitsColumns[key] = null; });
const ignoredFoodWeightColumns = ['unitId'];
const renamedFoodWeightColumns = {
conversionToG: 'Conversion to Grams',
};
this.preppedUnitColumns = TableColumnHelper([UnitsColumns], ignoredFoodWeightColumns, renamedFoodWeightColumns);
this.preppedUnitColumns[1].lookup = {
Volume: 'Volume',
Weight: 'Weight',
};
this.state = {
allUnits: props.allUnits,
};
this.clientUnitAPI = new UnitsAPI(this.props.token);
this.notificationBar = React.createRef();
}
onRowAdd = (row) => new Promise(async (res, rej) => {